﻿using UnityEngine;
using System;
using System.Collections;

namespace TCG
{
    /// <summary>
    /// 目前游戏中的资源有AssetBundle和Proto流两类，发出加载请求时必须指明。
    /// </summary>

    public enum AssetType
    {
        UnityAssetBundle,
        ProtoStream
	}

    /// <summary>
    /// 资源请求，返回的结果也在其中。
    /// </summary>

    public class AssetRequest
    {
        public AssetRequest (uint assetId, AssetType assetType, Action<AssetRequest> callback)
        {
            this.assetId = assetId;
            this.assetType = assetType;
			this.requester = callback;
        }

		public void Request<T> () where T : class
		{
			Type type = typeof (T);
			if (type == typeof (Texture))
			{
				GameAssetsDatabase.instance.textureDatabase.LoadAsset (this);
			}
			else if (type == typeof (Material))
			{
				GameAssetsDatabase.instance.materialDatabase.LoadAsset (this);
			}
			else if (type == typeof (GameObject))
			{
				GameAssetsDatabase.instance.prefabDatabase.LoadAsset (this);
			}
		}
        public uint assetId { get; private set; }
        public AssetType assetType { get; private set; }
        public Action<AssetRequest> requester { get; private set; }
		public string assetUrl { get; set; }

        public string error { get; set; }

        public GameAsset<T> GetResultAsset<T> () where T : class
        { 
            return m_result == null ? null : m_result as GameAsset<T>; 
        }

        public void SetResultAsset<T> (GameAsset<T> asset) where T : class
        {
            m_result = asset;
        }

        private System.Object m_result;
    }
}
